//
//  ComposeViewController.swift
//  weibo
//
//  Created by sanya on 15/7/6.
//  Copyright © 2015年 sanya. All rights reserved.
//

import UIKit
import FFAutoLayout
class ComposeViewController: UIViewController {


    private lazy var emoticonKeyboardVC:EmoticonViewController = EmoticonViewController { (emoticon) -> () in
        self.textView.insertEmoticon(emoticon)
    }
    // 关闭
    func close() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    // 发送
    func sendStatus() {
        print(textView.emoticonText())
    }
    /// 选择照片
    func selectPicture() {
        print("选择照片")
    }
    
    /// 切换视图
    func inputEmoticon() {
        // 如果输入视图为nil 说明使用的是系统键盘
        print(textView.inputView)
        // 切换键盘之前要先关闭键盘
        textView.resignFirstResponder()
        // 更换键盘输入视图
        textView.inputView = (textView.inputView == nil) ? emoticonKeyboardVC.view : nil
        // 重新设置焦点
        textView.becomeFirstResponder()
        
    }
    
    override func loadView() {
        view = UIView()
        view.backgroundColor = UIColor(white: 0.5, alpha: 1)
        addChildViewController(emoticonKeyboardVC)
        prepareNavigationBar()
        prepareTextView()
    }
    private func prepareToolBar() -> UIToolbar {
        let tb = UIToolbar(frame: CGRect(x: 0, y: 0, width: 0, height: 44))
        tb.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
//        tb.backgroundColor = UIColor.redColor()
        let itemSettings = [["imageName": "compose_toolbar_picture", "action": "selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]
        // 所有按钮的数组
        var items = [UIBarButtonItem]()
        for settings in itemSettings {
            items.append(UIBarButtonItem(imageName: settings["imageName"]!, highlightedImageName: nil, target: nil, actionName: settings["action"]))
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        tb.items = items
        items.removeLast()
        return tb
    }
    /// 准备输入文本
    private func prepareTextView() {
        view.addSubview(textView)
//        textView.text = "分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事..."
        textView.font = UIFont.systemFontOfSize(18)
        textView.ff_Fill(view)
        // 设置输入视图的辅助视图
        textView.inputAccessoryView = prepareToolBar()
        // 设置垂直滚动
        textView.alwaysBounceVertical = true
        // 设置滚动关闭键盘
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        // 设置站位标签
        placeHolderLabel.text = "分享新鲜事..."
        placeHolderLabel.sizeToFit()
        textView.addSubview(placeHolderLabel)
        placeHolderLabel.ff_AlignInner(ff_AlignType.TopLeft, referView: textView, size: nil, offset: CGPoint(x: 5, y: 8))
    }
    
    private func prepareNavigationBar() {
        // 1. 左侧按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        // 2. 右侧按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        // 3. 导航栏
        let titleView = UIView(frame: CGRectMake(0, 0, 100, 32))
        let label1 = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label1.text = "发微博"
        label1.sizeToFit()
        let label2 = UILabel(color: UIColor(white: 0.0, alpha: 0.5), fontSize: 13)
        label2.text = sharedUserAccount?.name ?? ""
        label2.sizeToFit()
        
        titleView.addSubview(label1)
        titleView.addSubview(label2)
        label1.ff_AlignInner(ff_AlignType.TopCenter, referView: titleView, size: nil)
        label2.ff_AlignInner(ff_AlignType.BottomCenter, referView: titleView, size: nil)
        navigationItem.titleView = titleView
    
    }
    /// 懒加载
    private lazy var textView = UITextView()
    private lazy var placeHolderLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 18)


}
